package com.atm.atmsys.Service.Imp;


import com.atm.atmsys.Mapper.TransactionMapper.TransactionMapper;
import com.atm.atmsys.Service.AccountService;
import com.atm.atmsys.Service.TransactionService;
import com.atm.atmsys.dto.TransactionPageByCardNo;
import com.atm.atmsys.dto.TransactionPageQueryDTO;
import com.atm.atmsys.dto.TransactionRecordDTO;
import com.atm.atmsys.entity.AdminInfo;
import com.atm.atmsys.entity.PageResult;
import com.atm.atmsys.entity.TransactionRecord;
import com.atm.atmsys.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TransactionServiceImp implements TransactionService {

    @Autowired
    private TransactionMapper transactionMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    HttpServletRequest request;

    // 添加交易记录
    @Override
    public void addTransaction(TransactionRecordDTO transactionRecordDTO) {
        TransactionRecord transactionRecord = new TransactionRecord();
        BeanUtils.copyProperties(transactionRecordDTO, transactionRecord);
        transactionRecord.setTranTime(LocalDateTime.now());
        transactionMapper.addTransaction(transactionRecord);
    }


    // 添加交易记录，不更新数据库
    @Override
    public void addTransactionWithoutChangeMysql(TransactionRecordDTO transactionRecordDTO) {
        TransactionRecord transactionRecord = new TransactionRecord();
        BeanUtils.copyProperties(transactionRecordDTO, transactionRecord);
        transactionRecord.setCardNo2(transactionRecordDTO.getCardNo1());
        transactionRecord.setTranTime(LocalDateTime.now());
        Object atmIdObj = request.getSession().getAttribute("atmId");
        int atmId = (Integer) atmIdObj;
        transactionRecord.setAtmId(atmId);
        if (transactionRecordDTO.getStatus().equals("save")){

            transactionMapper.saveTransaction(transactionRecord);

        }else if(transactionRecordDTO.getStatus().equals("withdraw")){

            transactionMapper.withdrawTransaction(transactionRecord);
        }else if (transactionRecordDTO.getStatus().equals("transfer")){
            transactionRecord.setCardNo2(transactionRecordDTO.getCardNo2());
            transactionMapper.transferTransaction(transactionRecord);

        }

    }

    @Override
    public void deleteTransaction(int id) {
        transactionMapper.deleteTransaction(id);
    }

    // 分页查询
    @Override
    public PageResult pageQuery(TransactionPageQueryDTO transactionPageQueryDTO) {
        PageHelper.startPage(transactionPageQueryDTO.getPage(), transactionPageQueryDTO.getPageSize());
        Page<TransactionRecord> page =transactionMapper.pageQuery(transactionPageQueryDTO);
        long total = page.getTotal();
        List<TransactionRecord> result = page.getResult();
        return new PageResult(total,result);
    }



    // 分页查询，根据卡号
    @Override
    public PageResult pageByCardNo(TransactionPageByCardNo transactionPageByCardNo) {
        PageHelper.startPage(transactionPageByCardNo.getPage(), transactionPageByCardNo.getPageSize());
        Page<TransactionRecord> page =transactionMapper.pageByCardNo(transactionPageByCardNo);
        long total = page.getTotal();
        List<TransactionRecord> result = page.getResult();
        return new PageResult(total,result);
    }
}
